package models;
import java.util.*;

import play.data.binding.As;

import javax.persistence.*;

import org.hibernate.annotations.Where;

import play.data.validation.*;
import play.db.jpa.*;
 
@Entity
public class Utilisateur extends Model 
{
	@Required
	@MaxSize(50)
	public String pseudo;
	@Required
	@MaxSize(20)
	public String mdp;
	@Required
	@MaxSize(50)
	public String email;
	
	public Date date_derniere_connexion;
	
	public boolean is_admin;
	
	@OneToMany(mappedBy="utilisateur", cascade=CascadeType.ALL)
	public List<Message> messages;
	
	@OneToMany(mappedBy="utilisateur", cascade=CascadeType.ALL)
	public List<Resultat> resultats;
	
	@OneToOne
	public Modele_bateau modele_bateau;
	
	//Ctor
	public Utilisateur(String pseudo, String mdp, String email, Date date_derniere_connexion, Modele_bateau modele_b, boolean is_admin) 
	{
		this.resultats = new ArrayList<Resultat>();
		this.messages = new ArrayList<Message>();
		this.pseudo = pseudo;
		this.mdp = mdp;
		this.email = email;
		this.date_derniere_connexion = date_derniere_connexion;
		this.modele_bateau = modele_b;
		this.is_admin = is_admin;
	}
	
	//Ctor sans modèle bateau
	public Utilisateur(String pseudo, String mdp, String email, Date date_derniere_connexion, boolean is_admin) 
	{
		this.resultats = new ArrayList<Resultat>();
		this.messages = new ArrayList<Message>();
		this.pseudo = pseudo;
		this.mdp = mdp;
		this.email = email;
		this.date_derniere_connexion = date_derniere_connexion;
		this.is_admin = is_admin;
	}	
	
	//Ctor
	public Utilisateur(String pseudo, String mdp, String email,
			Date date_derniere_connexion, boolean is_admin,
			List<Message> messages, List<Resultat> resultats,
			Modele_bateau modele_bateau) 
	{
		this.pseudo = pseudo;
		this.mdp = mdp;
		this.email = email;
		this.date_derniere_connexion = date_derniere_connexion;
		this.is_admin = is_admin;
		this.messages = messages;
		this.resultats = resultats;
		this.modele_bateau = modele_bateau;
	}


	public static Utilisateur connect(String pseudo, String password)
	{
		return find("byPseudoAndMdp", pseudo, password).first();
	}	
	
	
	//Retourne la liste des courses auxquelles le joueurs participe
	public List<Course> getCourseParicipe()
	{
		List<Course> courses = new ArrayList<Course>();
		
	    List<Bateau> bateaux = this.modele_bateau.bateaux;
	    Iterator<Bateau> itr = bateaux.iterator(); 
	    while(itr.hasNext()) 
	    {
	        Bateau bateau = itr.next(); 
	        List<Participe> participes = bateau.participes;
	        Iterator<Participe> itr2 = participes.iterator();
	        while(itr2.hasNext())
	        {
	        	Participe participe = itr2.next();
	        	courses.add(participe.course);	        	
	        }
	    } 
	    
	    return courses;
	}
	
	//verifie qu'un joueur participe à une course
	public boolean participeCourse(long course_id, long util_id)
	{
		List<Course> courses = Course.find("select c " +
				"from Course c, Participe p, Bateau b , Modele_bateau m, Utilisateur u " +
				"where p.course = c and " +
				"p.bateau = b and " +
				"b.modele = m and " +
				"m.utilisateur = u and " +
				"u.id = ? and " +
				"c.id = ?" , util_id, course_id).fetch();
		
	    return !courses.isEmpty();
	}
	
	// To string
	public String toString()
	{
		return this.pseudo;
	}
}
